﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Framework.Core.Condition
{
    public class ComparsionExpression<T> : IExpression where T : IComparable
    {
        #region 变量与属性
        private ComparsionExpressionItem<T> _LeftExpressionItem;

        public ComparsionExpressionItem<T> LeftExpressionItem
        {
            get
            {
                return this._LeftExpressionItem;
            }
            set
            {
                this._LeftExpressionItem = value;
            }
        }

        private ComparsionExpressionItem<T> _RightExpressionItem;

        public ComparsionExpressionItem<T> RightExpressionItem
        {
            get
            {
                return this._RightExpressionItem;
            }
            set
            {
                this._RightExpressionItem = value;
            }
        }

        private ComparsionOperation<T> _LogicOperator;

        public ComparsionOperation<T> LogicOperator
        {
            get
            {
                return this._LogicOperator;
            }
            set
            {
                this._LogicOperator = value;
            }
        }

        private ExpressionType _ExpressionType = ExpressionType.ExpressionAndExpressionItem;
        #endregion

        public bool GetResult()
        {
            if (this.LeftExpressionItem == null
                && this.RightExpressionItem == null)
            {
                throw new Exception("Expressions或ExpressionItems其中一个属性不能为null。");
            }

            switch (this._ExpressionType)
            {
                case ExpressionType.TwoExpressionItems:
                    return this.LogicOperator.Calculate(this.LeftExpressionItem, this.RightExpressionItem);
                default:
                    return false;
            }
        }

        #region 构造函数
        public ComparsionExpression(ComparsionExpressionItem<T> leftExpressionItem, ComparsionExpressionItem<T> rightExpressionItem, ComparsionOperation<T> logicOperator)
        {
            this._LeftExpressionItem = leftExpressionItem;
            this._RightExpressionItem = rightExpressionItem;
            this._LogicOperator = logicOperator;
            this._ExpressionType = ExpressionType.TwoExpressionItems;
        }

        public ComparsionExpression()
        {
        }
        #endregion

        #region 显式实现
        IOperation IExpression.LogicOperator
        {
            get
            {
                return this.LogicOperator;
            }
            set
            {
                this.LogicOperator = (ComparsionOperation<T>)value;
            }
        }

        IExpression IExpression.LeftExpression
        {
            get
            {
                throw new Exception("The method or operation is not implemented.");
            }
            set
            {
                throw new Exception("The method or operation is not implemented.");
            }
        }

        IExpression IExpression.RightExpression
        {
            get
            {
                throw new Exception("The method or operation is not implemented.");
            }
            set
            {
                throw new Exception("The method or operation is not implemented.");
            }
        }

        IExpressionItem IExpression.LeftExpressionItem
        {
            get
            {
                return this.LeftExpressionItem;
            }
            set
            {
                this.LeftExpressionItem = (ComparsionExpressionItem<T>)value;
            }
        }

        IExpressionItem IExpression.RightExpressionItem
        {
            get
            {
                return this.RightExpressionItem;
            }
            set
            {
                this.RightExpressionItem = (ComparsionExpressionItem<T>)value;
            }
        }
        #endregion
    }
}
